const app = getApp()
const login_js = require('../../utils/login.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_user: false,
    is_dark: false,
    modal_show: false,
    model_idx: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      is_dark: app.global_data.darkness
    })
  },
  onShow() {
    // 检查是否登录过
    if (app.global_data.user_data.openid) {
      this.setData({
        is_user: true
      })
    }
    this.setData({
      is_dark: app.global_data.darkness
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  ...login_js.methods,
  /**
   * 登录时候获取用户信息回调
   */
  async getUserInfo(e) {
    await this.getProfile()
    this.clickModalCancel()
  },
  clickModalCancel() {
    this.setData({
      is_user: true,
      modal_show: false,
    })
  },
  clickGame(e) {
    if (this.data.is_user) {
      let {
        idx
      } = e.currentTarget.dataset
      let url
      switch (idx) {
        case 'twenty_four_math':
          url = '/pages/twenty_four_math/twenty_four_math'
          break
        case 'sudoku':
          url = '/pages/sudoku/sudoku'
          break
        case 'klotski':
          url = '/pages/klotski/klotski'
          break
        default:
          break
      }
      wx.navigateTo({
        url: url
      })
    } else {
      this.setData({
        modal_show: true
      })
    }
  },
  clickModel(e) {
    if (this.data.is_user) {
      let {
        idx
      } = e.currentTarget.dataset
      this.setData({
        model_idx: idx
      })
    } else {
      this.setData({
        modal_show: true
      })
    }
  },
  clickRank(e) {
    let {
      idx
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/minigame_rank/minigame_rank?type=${idx}`
    })
  },
  diyTest() {
    if (this.data.is_user) {
      wx.navigateTo({
        url: `/pages/twenty_four_math_diy/twenty_four_math_diy`
      })
    } else {
      this.setData({
        modal_show: true
      })
    }
  }
})